
import CommonUtil from "../CommonUtil";
import FrameBase from "../FrameBase";

export class FrameVoiceMgr extends FrameBase {
    private static _instance: FrameVoiceMgr;
    public static get instance(): FrameVoiceMgr {
        if (!this._instance) {
            this._instance = new FrameVoiceMgr();
        }
        return this._instance;
    }

    // protected _tag: string = "FrameVoiceMgr";

    private _isInit: boolean = false;
    private _audioSourceMusic!: cc.AudioSource;
    private _audioSourceEffect!: cc.AudioSource;

    private _musicConfig!: IMusicConfig; // 音乐配置
    private _effectConfig!: IMusicConfig; // 音效配置
    private _clickSound!: cc.AudioClip; // 点击音效

    public vibrateSwitch: boolean = true;

    public init() {
        if (!this._audioSourceMusic) {
            this._audioSourceMusic = new cc.AudioSource();
        }

        if (!this._audioSourceEffect) {
            this._audioSourceEffect = new cc.AudioSource();
        }

        this._musicConfig = {
            clip: null,
            volume: 1,
            close: false
        }

        this._effectConfig = {
            clip: null,
            volume: 1,
            close: false
        }
        this._audioSourceMusic.volume = this._musicConfig.volume;
        this._audioSourceMusic.loop = true;
        this._audioSourceEffect.volume = this._effectConfig.volume;
        this._audioSourceEffect.loop = false;
        this._isInit = true;
    }

    /**
     * 设置点击音效
     * @param clip 点击音效的音乐片段资源
     */
    public setClickSound(clip: cc.AudioClip) {
        this._clickSound = clip;
    }

    public async playMusic(clip: cc.AudioClip | string) {
        if (!clip) {
            // this.Error("clip is null")
            return
        }
        this._audioSourceMusic.stop();
        if (this._musicConfig.close) {
            return
        }

        if (!this._isInit) {
            // this.Error(`plase call init function in ${this._tag} script`);
            return
        }


        if (clip instanceof cc.AudioClip) {
            this._audioSourceMusic.clip = clip;
        } else {
            this._audioSourceMusic.clip = await CommonUtil.loadVoice(clip);
        }
        this._musicConfig.clip = this._audioSourceMusic.clip;
        this._audioSourceMusic.play();
    }

    public async playEffect(clip: cc.AudioClip | string) {
        if (!clip) {
            // this.Error("clip is null")
            return
        }

        if (this._effectConfig.close) {
            return
        }

        if (!this._isInit) {
            // this.Error(`plase call init function in ${this._tag} script`);
            return
        }

        if (clip instanceof cc.AudioClip) {
            this._audioSourceEffect.clip = clip;
        } else {
            this._audioSourceEffect.clip = await CommonUtil.loadVoice(clip);
        }
        this._effectConfig.clip = this._audioSourceEffect.clip;
        this._audioSourceEffect.play();
    }

    public set musicVolum(n: number) {
        this._musicConfig.volume = n;
        this._audioSourceMusic.volume = this._musicConfig.volume;
    }
    public get musicVolum() {
        return this._audioSourceMusic.volume
    }

    public set effectVolum(n: number) {
        this._effectConfig.volume = n;
        this._audioSourceEffect.volume = this._effectConfig.volume;
    }
    public get effectVolum() {
        return this._audioSourceEffect.volume
    }

    public setMusicSwitch() {
        this._musicConfig.close = !this._musicConfig.close;
        this.playMusic(this._musicConfig.clip);
    }

    public setEffectSwitch() {
        this._effectConfig.close = !this._effectConfig.close;
        this.playEffect(this._effectConfig.clip);
    }

    public get musicSwitch() {
        return this._musicConfig.close;
    }

    public get effectSwith() {
        return this._effectConfig.close;
    }
}

interface IMusicConfig {
    clip: cc.AudioClip,
    volume: number,
    close: boolean
}